<?php

namespace app\common\arvato;
use app\common\common\Helper;
use think\Config;
use app\common\common\Constants;

/**
 * Description of CommonCurl
 *
 * @author Leo.xie
 */
class CommonCurl extends Base {
    
    /**
     * 通过请求soa2，来请求arvato接口
     * @param type $json
     * @return type
     * @throws \Exception
     */
    private function curlArvato($json){
        $helper = new Helper();
        try{
            $url = Config::get('SOA2_IN_URL');
            $result = $helper->HttpPostJson($url, $json);
            $res = json_decode($result,true);
            if($res['code'] == 200 && $res['msg'] == 'success'){
                return $res['data'];
            }
            throw new \Exception("request soa2 curl arvato :{$res['msg']}");
            
        }catch(\Exception $ex){
            throw new \Exception('error:('.$ex->getMessage().')');
        }
    }
    
    /**
     * 同步请求soa2的接口,返回的是接口的data部分
     * @param string $function
     * @param array $data
     */
    public function curlSoa2($function='',$data=[]){
        try{
            if(empty(Constants::$pushArvartoFunc[$function])){
                throw new \Exception("Constants don't find the function");
            }
            
            $input = [
                'function'=>Constants::$pushArvartoFunc[$function],
                'data'=>$data
            ];

            $json = json_encode($input);
            return $this->curlArvato($json);
            
        } catch (\Exception $ex){
            throw new \Exception($ex->getMessage());
        }
    }
    
}
